package com.ruoyi.ylyx.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.CommonRpc;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.ylyx.domain.*;
import com.ruoyi.ylyx.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 轮播Controller
 *
 */
@Controller
@RequestMapping("/phone/ylyx/user")
public class YlyxUserPhoneController extends BaseController
{

    @Autowired
    private IYlyxUserService userService;

    @Resource
    private CommonRpc commonRpc;

    @Autowired
    private ISysDtAreaService sysDtAreaService;

    @Autowired
    private IYlyxBaseconfigService ylyxBaseconfigService;

    @Autowired
    private IYlyxMessageService ylyxMessageService;

    /**
     * 用户注册
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public AjaxResult register(String userAccount, String userPassword, String userName, String userIdcard){
        YlyxUser user = new YlyxUser();
        user.setUserAccount(userAccount);
        List<YlyxUser> list = userService.selectYlyxUserList(user);
        if(list.size() > 0){
            return error("账号已存在");
        }
        user.setUserPassword(userPassword);
        user.setUserName(userName);
        user.setUserIdcard(userIdcard);
        user.setId(IdWorker.getId());
        userService.insertYlyxUser(user);
        return success("注册成功", user);

    }

    /**
     * 发送短信验证码
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/sendTextMsg")
    @ResponseBody
    public AjaxResult sendTextMsg(HttpServletRequest request, HttpServletResponse response){
        String phone = request.getParameter("phone");
        if(EmptyUtils.isEmpty(phone)){
            return  error("手机号为空");
        }
        return success("发送成功", commonRpc.sendBatchSms(phone));
    }

    /**
     * 用户注册--手机号注册
     * @return
     */
    @RequestMapping("/registerPhone")
    @ResponseBody
    public AjaxResult registerPhone(String userPhone, String userPassword, String userName, String userIdcard){
        YlyxUser user = new YlyxUser();
        user.setUserAccount(userPhone);
        List<YlyxUser> list = userService.selectYlyxUserList(user);
        if(list.size() > 0){
            return error("账号已存在");
        }
        user.setUserPhone(userPhone);
        user.setUserPassword(userPassword);
        user.setUserName(userName);
        user.setUserIdcard(userIdcard);
        user.setId(IdWorker.getId());
        userService.insertYlyxUser(user);
        return success("注册成功", user);

    }



    /**
     * 登陆
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public AjaxResult login(String userAccount, String userPassword){
        YlyxUser user = new YlyxUser();
        user.setUserAccount(userAccount);
        List<YlyxUser> list = userService.selectYlyxUserList(user);
        if(list.size() == 0){
            return error("账号不存在");
        }
        user = list.get(0);
        if("是".equals(user.getIsDisable()) || "1".equals(user.getIsDisable())){
            return error("账号被禁用");
        }
        if(userPassword.equals(user.getUserPassword())){
            return success("登陆成功", user);
        }
        return error("密码错误");
    }


    /**
     * 修改用户信息
     * @return
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public AjaxResult updateUser(String userId, String userPassword, String userPhone, String userSex, Date userBirth,
            String userTel, String userProvincecode, String userProvincename, String userCitycode, String userCityname, String userAddress, String userPostcode){
        YlyxUser user = new YlyxUser();
        user.setId(userId);
        if(EmptyUtils.isNotEmpty(userPassword)) user.setUserPassword(userPassword);
        if(EmptyUtils.isNotEmpty(userPhone)) user.setUserPhone(userPhone);
        if(EmptyUtils.isNotEmpty(userSex)) user.setUserSex(userSex);
        if(EmptyUtils.isNotEmpty(userBirth)) user.setUserBirth(userBirth);
        if(EmptyUtils.isNotEmpty(userTel)) user.setUserTel(userTel);
        if(EmptyUtils.isNotEmpty(userProvincecode)) user.setUserProvincecode(userProvincecode);
        if(EmptyUtils.isNotEmpty(userProvincename)) user.setUserProvincename(userProvincename);
        if(EmptyUtils.isNotEmpty(userCitycode)) user.setUserCitycode(userCitycode);
        if(EmptyUtils.isNotEmpty(userCityname)) user.setUserCityname(userCityname);
        if(EmptyUtils.isNotEmpty(userAddress)) user.setUserAddress(userAddress);
        if(EmptyUtils.isNotEmpty(userPostcode)) user.setUserPostcode(userPostcode);
        userService.updateYlyxUser(user);
        return success("操作成功", userService.selectYlyxUserById(userId));

    }

    /**
     * 获取省市县
     * @param pid
     * @return
     */
    @RequestMapping("/getAreaByPid")
    @ResponseBody
    public AjaxResult getAreaByPid(String pid){
        pid = EmptyUtils.isEmpty(pid)?"0":pid;
        SysDtArea area = new SysDtArea();
        area.setAreaParentId(pid);
        return success("获取成功", sysDtAreaService.selectSysDtAreaList(area));
    }


    /**
     * 获取系统参数
     * @return
     */
    @RequestMapping("/getBaseConfig")
    @ResponseBody
    public AjaxResult getBaseConfig(){
        return success("获取成功", ylyxBaseconfigService.selectYlyxBaseconfigById("1"));
    }


    /**
     * 提交留言
     * @return
     */
    @RequestMapping("/saveMessage")
    @ResponseBody
    public AjaxResult saveMessage(String userId, String content){
        YlyxMessage message = new YlyxMessage();
        message.setId(IdWorker.getId());
        message.setUserId(userId);
        message.setMessageContent(content);

        ylyxMessageService.insertYlyxMessage(message);
        return success("提交成功");
    }

    /**
     * 根据身份证获取用户信息
     * @param userIdcard
     * @return
     */
    @RequestMapping("/getUserByIdCard")
    @ResponseBody
    public AjaxResult getUserByIdCard(String userIdcard){
        YlyxUser user = new YlyxUser();
        user.setUserIdcard(userIdcard);
        List<YlyxUser> list = userService.selectYlyxUserList(user);
        if(list.size() > 0){
            return success("注册成功", list.get(0));
        }
        return error("身份证不存在");

    }

}
